const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const tool = require("../../tools/tool");
const router = new Router();
// app版本配送 注册
router.post('/appps/reg', async ctx => {
    let username = ctx.request.body.username
    let userpwd = ctx.request.body.userpwd
    username = tool.xe.trim(username)
    userpwd = tool.xe.trim(userpwd)
    if(!username) return ctx.err('请输入用户名')
    if(!userpwd) return ctx.err('请输入密码')
    let hasAdmin = await db.models['admin'].findOne({
        where: {
            username
        }
    })
    if(hasAdmin) return ctx.err('用户名已存在，请换一个')

    if(!hasAdmin) {
        let tenant = {
            name: username,
            stat: 1,
            state: '正常',
            ct: tool.getTime()
        }
        let ur = await db.transaction(async () => {
            let model = await db.models['tenant'].create(tenant)
            let user = {
                tenantId: model.id,
                username: username,
                token: tool.uuid(),
                name: username,
                userpwd: tool.crypto.MD5(userpwd),
                ctime: tool.getTime()
            }
            let usr = await db.models['admin'].create(user)
            return usr
        })
        return ctx.suc('', ur)
    }
})
router.post('/appps/login', async ctx => {
    let username = ctx.request.body.username
    let userpwd = ctx.request.body.userpwd
    username = tool.xe.trim(username)
    userpwd = tool.xe.trim(userpwd)
    if(!username) return ctx.err('请输入用户名')
    if(!userpwd) return ctx.err('请输入密码')

    let hasAdmin = await db.models['admin'].findOne({
        where: {
            username,
            userpwd: tool.crypto.MD5(userpwd)
        }
    })
    if(hasAdmin) {
        if(hasAdmin.disabled === 1) return ctx.err('用户已注销')
        return ctx.suc('', hasAdmin)
    }
    else {
        return  ctx.err('用户名密码错误')
    }

})
router.post('/appps/disableduser', async ctx => {
    let user = ctx.user
    let hasAdmin = await db.models['admin'].findOne({
        where: {
            id: user.id
        }
    })
    if(hasAdmin) {
        if(hasAdmin.disabled === 1) return ctx.err('用户已注销')
        hasAdmin.disabled = 1
        await hasAdmin.save()
        return ctx.suc('已注销')
    }
    else {
        return  ctx.err('已注销')
    }

})
router.post('/appps/changPwd', async ctx => {
    let user = ctx.user
    let old = ctx.request.body.old
    let newpwd = ctx.request.body.newPwd
    let hasAdmin = await db.models['admin'].findOne({
        where: {
            username: user.username,
            userpwd: tool.crypto.MD5(old)
        }
    })
    if(hasAdmin) {
        if(hasAdmin.disabled === 1) return ctx.err('用户已注销')
        hasAdmin.userpwd = tool.crypto.MD5(newpwd)
        await hasAdmin.save()
        return ctx.suc('更新成功')
    }
    else {
        return  ctx.err('旧密码错误')
    }
})
router.post('/appps/saveuser', async ctx => {
    let uuid = ctx.request.body.token
    let username = ctx.request.body.username
    let userpwd = ctx.request.body.userpwd
    let hasAdmin = await db.models['admin'].findOne({
        where: {
            token: uuid
        }
    })
    // hasAdmin.username =
    return ctx.suc('', hasAdmin)

})
router.post('/appps/savebills', async ctx => {
    let user = ctx.user
    let formData = ctx.request.body.formData
    let id = ctx.request.body.id
    formData.splice(formData.length - 1, 1)
    if(id) {
        let model = await db.models['appPs'].findOne({
            id
        })
        model.ps = JSON.stringify(formData)
        await model.save()
        return ctx.suc('', id)
    }
    else {
        let model = await db.models['appPs'].create({
            tenantId: user.tenantId,
            userId: user.id,
            ps: JSON.stringify(formData),
            ctime: tool.getTime()
        })
        return ctx.suc('', model.id)
    }

})
router.post('/appps/getHistory', async ctx => {
    let user = ctx.user
    let beginDate = ctx.request.body.beginDate
    let endDate = ctx.request.body.endDate
    let buName = ctx.request.body.buName
    let list = await db.models['appPs'].findAll({
        where: {
            userId: user.id
        }
    })
    return ctx.suc('', list)

})
router.post('/appps/saveFeed', async ctx => {
    let user = ctx.user
    let content = ctx.request.body.content
    let model = {
        tenantId: user.tenantId,
        userId: user.id,
        ctime: tool.getTime(),
        msg: content
    }
    await db.models['appFeed'].create(model)
    return ctx.suc('提交成功')

})
// 拜访记录

module.exports = router;
